import xml.etree.ElementTree as ET
from .params import *
from .trip import Trip
from .ev import EV

class EVDict(dict[str,EV]):
    def __init__(self,file_path=None):
        super().__init__()
        if file_path is None: return
        for veh in ET.ElementTree(file = file_path).getroot():
            trips:list[Trip]=[]
            for trip in veh:
                route=trip.attrib["route_edges"].split(' ')
                trips.append(Trip(trip.attrib["id"],int(float(trip.attrib["depart"])),route[0],route[-1]))
            params = {
                "id":veh.attrib["id"],
                "trips":trips,
                "bcap":DEFAULT_FULL_BATTERY,
                "soc":DEFAULT_INIT_SOC,
                "consump":DEFAULT_CONSUMPTION,
                "efc_rate":DEFAULT_FAST_CHARGE_RATE,
                "esc_rate":DEFAULT_SLOW_CHARGE_RATE,
                "omega":DEFAULT_OMEGA,
                "krel":DEFAULT_KREL,
                "kfc":DEFAULT_FAST_CHARGE_THRESHOLD,
                "ksc":DEFAULT_SLOW_CHARGE_THRESHOLD,
                "max_v2g_rate":DEFAULT_MAX_V2G_RATE,
                "kv2g":DEFAULT_KV2G,
                "eta_c":DEFAULT_ETA_CHARGE,
                "eta_d":DEFAULT_ETA_DISCHARGE,
            }
            params2 = veh.attrib
            params2.pop("id")
            params.update({k:float(v) for k,v in params2.items()})
            assert params["id"] is not None
            self.add(EV(**params))

    def add(self,ev:EV):
        '''添加车辆'''
        super().__setitem__(ev.ID,ev)

    def pop(self,veh_id:str)->EV:
        '''根据ID删除车辆, 返回: 删除的值'''
        return super().pop(veh_id)